<template>
    <Modal v-model="visible" :title="title" @on-ok="ok" :mask-closable="false">
        <Form ref="form" :model="form" :label-width="100" onsubmit="return false">
            <FormItem label="项目名称">
                <Select v-model="form.projectId" clearable>
                    <Option v-for="item in projects" :value="item.id" :key="item.id">{{ item.name }}</Option>
                </Select>
            </FormItem>
        </Form>

        <div slot="footer">
            <Button type="text" size="large" @click.native="cancel">取消</Button>
            <Button type="primary" size="large" :loading="loading" @click.native="ok">确定</Button>
        </div>
    </Modal>
</template>

<script>
    import api from '../../api';

    export default {
        props: ['getData'],
        data () {
            return {
                visible: false,
                loading: false,
                title: '',
                form: {},
                projects: [],
                packageGroups: []
            };
        },
        methods: {
            ok () {
                this.loading = true;
                api.systemUserUpdate(this.form).then(data => {
                    this.loading = false;

                    if (data.code === 0) {
                        this.getData();
                        this.visible = false;
                    }
                });
            },
            open (title, id) {
                this.$refs.form.resetFields();
                this.visible = true;
                this.title = title;
                this.form.id = id;

                if (id) {
                    api.systemUserDetail({id: id}).then(data => {
                        if (data.code === 0) {
                            this.form = data.data;
                            this.form.password = '';
                        }
                    });
                }
                this.projectIndexNoPage;
            },
            cancel () {
                this.visible = false;
            }
        },
        computed: {
            projectIndexNoPage () {
                api.projectList().then(data => {
                    if (data.code === 0) {
                        this.projects = data.data;
                    }
                });
            }
        }
    };
</script>
